import 'package:baselib/utils/refresh_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:framelib/base/base_view.dart';
import 'package:framelib/utils/color_util.dart';
import 'package:framelib/utils/px_util.dart';
import 'package:framelib/widgets/comm_app_bar.dart';
import 'package:framelib/widgets/page_widget_manager.dart';
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:module_business/index/viewmodel/list_view_model.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/**
 * 列表
 */
class ListPage extends GetWidget<ListViewModel> {
  @override
  Widget build(BuildContext context) {
    return PageRouteBuild<ListViewModel>(builder: (viewModel) {
      print('--------------->ListPage,builder');
      return PageWidgetManager.buildPage(
          context: context,
          appBar: CommAppBar(
            elevation: px_0d5.sp,
            title: Text(
              '列表',
            ),
            actions: [
              SizedBox(width: px_0.w,)
            ],
          ),
          backgroundColor: color_f2f2f2,
          body: CommRefreshUtil.init(
            viewModel.controller,
            onRefresh:() => viewModel.onRefresh(pageCur: viewModel.page = 1),
            onLoad:() => viewModel.onRefresh(pageCur: viewModel.page + 1),
            onReFreshFunction: () => viewModel.onRefresh(pageCur: viewModel.page = 1),
            childWidget: buildListUi(viewModel, context),
          )
      );
    });
  }

  Widget buildListUi(ListViewModel viewModel, BuildContext context) {
    print('------->viewModel.dataLists?.length=${viewModel.dataLists?.length}');
    return ListView.builder(
        padding: EdgeInsets.only(),
        itemCount: viewModel.dataLists?.length ?? 0,
        itemBuilder: (BuildContext context, int index) {
          var dataItem = viewModel.dataLists[index];
          return Container(
            margin: EdgeInsets.only(
              left: px_12.w,
              right: px_12.w,
              top: index == 0 ? px_12.h : px_0.h,
              bottom: px_12.h,
            ),
            padding: EdgeInsets.only(
              left: px_20.w,
              right: px_20.w,
              top: px_36.h,
              bottom: px_36.h,
            ),
            decoration: BoxDecoration(
                color: color_white,
                borderRadius: BorderRadius.circular(px_6.sp)),
            child: Text(
                'item-$dataItem'
            ),
          );
        });
  }

}
